
***************************************
* 								Overview: Readme!
***************************************
/*

This do file runs all of the required code to produce the tables and graphs of 
Risk Taking With Social Consequences by Paul Clist, Ben D'Exelle & Arjan Verschoor 

The article is published open access at the The Journal of Development Studies
link: https://doi.org/10.1080/00220388.2025.2453523

If you spot any problems please email Paul Clist
Email: paul.clist@uea.ac.uk
Website: https://paulclist.github.io/

It is organised to match the article/supplementary materials order, as: 

Prep - install packages
Prep - Change working Directory
Manuscript
	- Table 4
	- Test reported in text in 4.0
	- Table 5
	- Figure 2
	- Table 6
	- Supplementary Material Table 4 & Figure 3 main text: Multinomial Logit
	- Table 7

Supplementary Materials Table 1 & 2
Supplementary Materials Table 3
Supplementary Materials Figure 1
Supplementary Materials Table 5
Supplementary Materials Table 6
Supplementary Materials Table 7
 */


***************************************
* 								Prep: install any packages you need
***************************************

/*
* Install git so you can install plottig easily, which is a graph style. 
* If you don't want to replicate the style of the graphs you can skip these three steps. 
ssc install git
git install https://github.com/danbischof/plottig
set scheme plottig

ssc install estout, replace // package to help save and format results
*/


***************************************
* 								Prep: Change Working directory
***************************************

** set working directory
*cd "where you saved the files"


***************************************
* 								Table 4: summary stats
***************************************

* load data
use "prepared.dta", clear

est clear
estpost tabstat age female education business land decisions1 decs2positive decs2negative ///
decs2idio decs3positive decs3negative decs3idio,  stat(mean  sd min max) col(statistics)
esttab . using temp.tex, cells("mean(fmt(a2)) sd(fmt(a2)) min max") replace

estpost tab education
esttab . using temp3.tex, cells("pct(fmt(a1))") replace

estpost tab religion
esttab . using temp2.tex, cells("pct(fmt(a1))") replace


***************************************
* 								Section 4.0
***************************************
* cautious shift (mentioned in text)
ttest decisions1 =decs2positive
signrank   decisions1 =decs2positive


***************************************
* 								Table 5: Classifications
***************************************
tab dif_r2
tab class_r2

tabi 83 106 130  \ 40 140 140 , chi2 //	 different from chance?
di r(p) 

* table 5 last line. 
count if decisions1==decs2positive & class_r2==2
count if decisions1==decs2positive & class_r2==1
count if decisions1==decs2positive & class_r2==3


***************************************
* 								Figure 2
***************************************

proportion class_co class_no class_r2 class_r3 class_r2 class_idio_neg_r2 class_pos_idio_r2, vce(cluster village_id)
forvalues i=1/3{
test `i'.class_co=`i'.class_no
test `i'.class_r2=`i'.class_r3
test `i'.class_co=`i'.class_idio_neg_r2 
test `i'.class_co=`i'.class_pos_idio_r2
}

* for p values on graph - handcoded for placement
test 2.class_co=2.class_no
test 3.class_co=3.class_no

test 2.class_r2=2.class_r3
test 3.class_r2=3.class_r3

test 2.class_co=2.class_idio_neg_r2 
test 2.class_co=2.class_pos_idio_r2
test 3.class_co=3.class_idio_neg_r2 
test 3.class_co=3.class_pos_idio_r2
 
set scheme plottig							// Commands for graph style  - can delete if not working for you. 
* grstyle init
graph set window fontface "Times New Roman"

preserve												// graph
expand 6 
sort unique_id 
gen temp=1
replace temp=temp[_n-1]+1 if unique_id==unique_id[_n-1]

gen class=.
replace class= class_co 			if temp==1 
replace class= class_no 			if temp==2
replace class= class_r2 			if temp==3 
replace class= class_r3 			if temp==4
replace class= class_idio_neg_r2   	if temp==5
replace	class= class_pos_idio_r2 	if temp==6

 label define types 1 "Indifferent" 2 "Averse" 3 "Seeking", replace
label values class types
label values temp temporary  

forvalues i=1/6 {
proportion class if temp==`i', vce(cluster village_id)
est sto a`i'
}
coefplot (a1, msym(Oh)) (a2, msym(O))  ,   ///
 citop xlabel(none)  xtick(0(.1).5, grid) xscale(r(.5)) ///
plotlabels("Same Village" "Different Village" ) ///
legend(textwidth(20rs) pos(2)  col(1) subtitle("{bf:By Partner}", j(left) size(small)) ) ///
 nodraw name(aa, replace)  ///
text(1.95 .455 "} p=0.61" 2.95 .495 "} p=0.59", si(small)) fysize(60)

coefplot (a3, msym(Dh)) (a4, msym(D)) ,   ///
 citop  xlabel(none)  xtick(0(.1).5, grid) xscale(r(.5)) ///
plotlabels("Round 2" "Round 3" ) ///
legend(textwidth(20rs) pos(2) col(1)   subtitle("{bf:By Round}", j(left) size(small))   ) ///
 nodraw name(bb, replace) ///
text(1.95 .49 "} p=0.13" 2.95 .49 "} p=0.27", si(small)) fysize(60)   

coefplot (a5, msym(Sh)) (a3, msym(S)) (a6, msym(Sh)),   ///
 citop  xlabel(0(.1).5)   ///
plotlabels( "i/-, Round 2" "+/-, Round 2" "+/i, Round 2") ///
legend(textwidth(20rs) pos(2)  col(1)  subtitle("{bf:By Definition}",  j(left)  size(small)) ) ///
 nodraw  name(cc, replace)  xscale(r(.5)) xtitle("Proportion") ///
text(2.15 .46 "} p=0.61" 1.75 .46 "} p=0.64" 3.15 .51 "} p=0.66" 2.75 .49 "} p=0.24", si(small)) ///
 fysize(90) 

gr combine aa bb cc, col(1) xsize(6) scale(1.55) imargin(tiny) 
graph export "robust.pdf", as(pdf) name("Graph") replace	

restore


***************************************
* 								Supplementary Material Table 4 & Figure 3 main text: Multinomial Logit
***************************************

preserve
expand 6 
sort unique_id 
gen temp=1
replace temp=temp[_n-1]+1 if unique_id==unique_id[_n-1]
gen 	class= class_r2 
replace class= class_idio_neg_r2   	if temp==2
replace	class= class_pos_idio_r2 	if temp==3
replace class= class_r3 			if temp==4
replace class= class_idio_neg_r3 	if temp==5
replace class= class_pos_idio_r3 	if temp==6
label values class types
label define temporary 1 "r2" 2 "r2 i -" 3 "r2 + i" 4 "r3" 5 " r3 i/-" 6  "+/i"
label values temp temporary  
gen r2=0
replace r2=1 if temp<4
label variable r2 "Round 2"
gen definition = 0
replace definition =1 if temp==2 | temp==5
replace definition =2 if temp==3 | temp==6
gen 	co=0
replace co=1 if covillages2==1 & temp<4
replace co=1 if covillages2==0 & temp>4
label variable co "Partner: CoVillager"
tab female class, row chi
di  `r(p)'


mlogit class r2 if temp==1 | temp==4 , base(1) vce(cluster village_id)
mlogit class co if temp==1 | temp==4 , base(1) vce(cluster village_id)
mlogit class i.def if temp<4 , base(1) vce(cluster village_id)
***



mlogit class age female education land business i.religion  decisions1 r2 i.definition co ///
, base(1) vce(cluster village_id)
est sto A
margins, dydx(age female education land business 2.religion 3.religion 4.religion 5.religion decisions1 r2 i.definition co ) post
est sto B

coefplot (, keep(*:1._predict) label(Indifferent) 	 msym(O)) ///
         (, keep(*:2._predict) label(Averse)	 msym(S))   ///
         (, keep(*:3._predict) label(Seeking) msym(D))  ///
    , swapnames xline(0) legend(rows(3)) scale(1.1)  xtitle("Percentage Points")  ///
	coeflabels(1.definition= "Definition: i/-" 2.definition= "Definition: +/i" ) ///
	name(dec6, replace) xsize(7) ysize(5) xlabel(-.2 "-20" -.1 "-10" 0 "0" .1 "10" .2 "20")

graph export "mlogit.pdf", as(pdf) replace
esttab A B using temp.tex , replace
restore



***************************************
* 								Table 6: Gender
***************************************
foreach var of varlist class_co class_no class_r2 class_r3 class_idio_neg_r2 class_pos_idio_r2 {
tab female `var' , chi2 row
di r(p)
}


***************************************
* 								Table 7: Gender
***************************************
 count if class_r2==1 * female==1 &  decisions1 ==decs2positive
local ineqindiff_female = r(N)
count if class_r2==1 * female==1 
local exante_female = r(N)
di 100*`ineqindiff_female'/`exante_female'

 count if class_r2==1 * female!=1 &  decisions1 ==decs2positive
local ineqindiff_male = r(N)
count if class_r2==1 * female!=1 
local exante_male = r(N)
di 100*`ineqindiff_male'/`exante_male'

 count if class_r3==1 * female==1 &  decisions1 ==decs3positive
local ineqindiff_female = r(N)
count if class_r3==1 * female==1 
local exante_female = r(N)
di 100*`ineqindiff_female'/`exante_female'

 count if class_r3==1 * female!=1 &  decisions1 ==decs3positive
local ineqindiff_male = r(N)
count if class_r3==1 * female!=1 
local exante_male = r(N)
di 100*`ineqindiff_male'/`exante_male'

di "women in total " (20/47+14/34)/2
di "men in total " (20/36+20/45)/2


gen temp1=.
foreach var of varlist decisions1 decs*{
replace temp1=`var'
qui estpost ttest   temp1, by(female)
est sto AAA`var'
qui ranksum temp1, by(female)
estadd scalar BBB_z=r(z)
estadd scalar BBB_p=r(p)
}
esttab AAA*  using temp.tex, star(* .1 ** .05 *** .01) t(a2) b(a2) noobs replace stats(BBB_z BBB_p)
drop temp1
tab female, su(decs3positive)
di ( 3.7596154 -3.855814 )/ 2.241805




***************************************
* 								Supplementary Materials: Table 1 & 2
***************************************

* table 1: 
tab decs2positive decs2negative
tab decs3positive decs3negative

* table 2: 
tab dec_co_pos dec_co_neg
tab dec_no_pos dec_no_neg


***************************************
* 								Supplementary Materials: Table 3
***************************************

// p values
symmetry class_r2  class_r3   
symmetry class_idio_neg_r2 class_idio_neg_r3 
symmetry class_pos_idio_r2 class_pos_idio_r3 

symmetry class_co  class_no    
symmetry class_co_idio_neg class_no_idio_neg
symmetry class_co_pos_idio class_no_pos_idio 

symmetry class_r2 			class_idio_neg_r2   
symmetry class_r3 			class_idio_neg_r3    
symmetry class_co 			class_co_idio_neg    
symmetry class_no 			class_no_idio_neg    

symmetry class_pos_idio_r2 	class_idio_neg_r2    
symmetry class_pos_idio_r3 	class_idio_neg_r3    
symmetry class_co_pos_idio 	class_co_idio_neg    
symmetry class_no_pos_idio 	class_no_idio_neg    

symmetry class_pos_idio_r2 	class_r2   			 
symmetry class_pos_idio_r3 	class_r3   			 
symmetry class_co_pos_idio 	class_co   			 
symmetry class_no_pos_idio 	class_no   			 


// simple %s
qui count if class_co==  class_no						//  by partner/round, within definition
di r(N)/319 
qui count if class_co_idio_neg==  class_no_idio_neg					
di r(N)/319 
qui count if class_co_pos_idio==  class_no_pos_idio					
di r(N)/319 
qui count if class_idio_neg_r2==class_r2			//  by def, within round 2
di r(N)/319 
qui count if class_idio_neg_r2==class_pos_idio_r2
di r(N)/319 
qui count if class_pos_idio_r2==class_r2
di r(N)/319 
qui count if class_idio_neg_r3==class_r3			//  by def, within round 3
di r(N)/319 
qui count if class_idio_neg_r3==class_pos_idio_r3
di r(N)/319 
qui count if class_pos_idio_r3==class_r3
di r(N)/319 
qui count if class_co_idio_neg==class_co			//  by def, within partner (co)
di r(N)/319 
qui count if class_co_idio_neg==class_co_pos_idio
di r(N)/319 
qui count if class_co==class_co_pos_idio
di r(N)/319 
qui count if class_no_idio_neg==class_no			//  by def, within partner (no)
di r(N)/319 
qui count if class_no_idio_neg==class_no_pos_idio
di r(N)/319 
qui count if class_no==class_no_pos_idio
di r(N)/319 


***************************************
* 								Supplementary Materials: Figure 1 
***************************************

 label define types 1 "Indifferent" 2 "Averse" 3 "Seeking", replace
label values class* types

 gen pweight = 319/215
 replace pweight=319/104 if gender==1
 
 est clear
 foreach var of varlist class_r2 class_r3 class_co class_no {
	eststo: proportion i.`var' 	
	eststo: proportion i.`var' if gender==1	
	eststo: proportion i.`var' if gender==2	
	eststo: proportion i.`var' [pweight=pweight]
  }
 
 coefplot est1  est2  est3  est4 , plotlabel("Unweighted" "Male" "Female" "Weighted" ) ///
 title("Round 2") nodraw name(r2, replace) vertical 
 coefplot est5  est6  est7  est8 , plotlabel("Unweighted" "Male" "Female" "Weighted" ) ///
  title("Round 3") nodraw name(r3, replace) vertical 
 coefplot est9  est10 est11 est12, plotlabel("Unweighted" "Male" "Female" "Weighted" ) ///
  title("Co Villagers") nodraw name(co, replace) vertical 
 coefplot est13 est14 est15 est16, plotlabel("Unweighted" "Male" "Female" "Weighted" ) ///
  title("Non CoVillagers")nodraw name(no, replace) vertical 
 
 gr combine r2 r3 co no, imargin(tiny) col(2) scale(1.5) ycommon
 
 ***************************************
* 								Supplementary Materials: Table 5 
***************************************

 
esttab est1  est2  est3  est4 , not nostar tex
esttab est5  est6  est7  est8, 	not nostar tex
esttab est9  est10 est11 est12, not nostar tex 
esttab est13 est14 est15 est16, not nostar tex
 

 
 
 
 ***************************************
 * 								Supplementary Materials: Table 6
***************************************


sum decs2positive
local N=`r(N)' 
count if decs2positive==decs2negative &  decs2positive==0
local col2=`r(N)' 
count if decs2positive==decs2negative &  decs2positive==7
local col3=`r(N)' 
local col4= `col2'+`col3'
count if decs2positive==decs2negative
local enumerator = `r(N)'
local col5 = 100*`enumerator'/`N'
local col6 = 100*(`enumerator'-`col4')/(`N'-`col4')
display `col2' " " `col3' " "  `col4' " "    %9.1f `col5'  " "    %9.1f `col6' 



local temporary "s2 s3 _co_ _no_"
        foreach x of local temporary {
qui sum dec`x'pos
local N=`r(N)' 
qui count if dec`x'pos==dec`x'neg &  dec`x'pos==0
local col2=`r(N)' 
qui count if dec`x'pos==dec`x'neg &  dec`x'pos==7
local col3=`r(N)' 
local col4= `col2'+`col3'
qui count if dec`x'pos==dec`x'neg
local enumerator = `r(N)'
local col5 = 100*`enumerator'/`N'
local col6 = 100*(`enumerator'-`col4')/(`N'-`col4')
display `col2' " " `col3' " "  `col4' " "    %9.1f `col5'  "% "    %9.1f `col6' "% "
		}



 ***************************************
 * 								Supplementary Materials: Table 7 
***************************************



***************************************
* noise, using the simulate command
***************************************

			 
* Its a bit easier to save four files: r2, r3, co and no
* then feed the file to the command 4 times, and summarise the results.  


forvalues i=2/3 {			
use "prepared.dta", clear
keep decs`i'positive decs`i'negative
rename decs`i'positive pos 
rename decs`i'negative negative
gen diff= pos-neg
keep diff pos neg
save r`i', replace
}

local partner "co no"
foreach x of local partner {
use "prepared.dta", clear
keep dec_`x'_pos dec_`x'_neg
rename dec_`x'_pos pos 
rename dec_`x'_neg negative
gen diff= pos-neg
keep diff pos neg
save `x', replace
}


program drop noise_types
program noise_types, rclass 
			 use co.dta, clear // run 4 times. the sim command won't play nicely with a loop 
*			 use no.dta, clear // remember to change the saving file at the end too
*			 use r2.dta, clear
*			 use r3.dta, clear
			 count if diff==0
			 return scalar exante =r(N)

			 count if diff<0
			 return scalar expost =r(N)

			 count if diff>0
			 return scalar efficiency =r(N)
			 
			 gen rando=runiform()
			 egen rank=rank(rando)

// random bit starts here, at 20%, i.e. 10 over and 10 below 
			 replace diff=diff+1 if rank<32.5
			 replace diff=diff-1 if rank>287
			 
			 count if diff==0
			 return scalar exante_e20 =r(N)

			 count if diff<0
			 return scalar expost_e20 =r(N)

			 count if diff>0
			 return scalar efficiency_e20 =r(N)
			 
// random : 40 
			 replace diff= pos-neg
			 replace diff=diff+1 if rank<64.5
			 replace diff=diff-1 if rank>256
			 
			 count if diff==0
			 return scalar exante_e40 =r(N)

			 count if diff<0
			 return scalar expost_e40 =r(N)

			 count if diff>0
			 return scalar efficiency_e40 =r(N)			 

// random : 60 
			 replace diff= pos-neg
			 replace diff=diff+1 if rank<96.5
			 replace diff=diff-1 if rank>224
			 
			 count if diff==0
			 return scalar exante_e60 =r(N)

			 count if diff<0
			 return scalar expost_e60 =r(N)

			 count if diff>0
			 return scalar efficiency_e60 =r(N)	

			 
end

simulate Exante=r(exante) expost=r(expost)  efficiency=r(efficiency) ///
exante_e20=r(exante_e20) expost_e20=r(expost_e20) efficiency_e20=r(efficiency_e20) ///
exante_e40=r(exante_e40) expost_e40=r(expost_e40) efficiency_e40=r(efficiency_e40) ///
exante_e60=r(exante_e60) expost_e60=r(expost_e60) efficiency_e60=r(efficiency_e60) ///
, reps(10000) nodots: noise_types

 save co_sim_results, replace // change these on each run thorugh the loop. 
*save no_sim_results, replace
*save r2_sim_results, replace
*save r3_sim_results, replace



** then knit back together. 

local allfour "co no r2 r3"
foreach x of local allfour {
qui use `x'_sim_results, clear
collapse Exante-efficiency_e60
save `x'_summary, replace
}	

clear 
append using co_summary no_summary r2_summary r3_summary
foreach var of varlist Exante-efficiency_e60 {
replace `var'=100*(`var'/319)
}
	

	
	*********************
	* fin
	*********************
